using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace EditorExtensions
{
    public class IMGUIEditorWindowExample : UnityEditor.EditorWindow
    {
        [MenuItem("EditorExtensions/02.IMGUI/01.GUILayoutExample")]
        static void OpenGuiLayoutExample()
        {
            GetWindow<IMGUIEditorWindowExample>().Show();
        }

        enum PageId
        {
            Basic,
            Enabled,
            Rotated,
            Scale,
            Color,
        }

        enum APIMode
        {
            GUI,
            GUILayout,
            EditorGUI,
            EditorGUILayout,
        }

        private GUILayoutAPI m_GUILayoutAPI = new GUILayoutAPI();
        private GUIAPI m_GUIAPI = new GUIAPI();
        private EditorGUIApi m_EditorGUIApi = new EditorGUIApi();
        private EditorGUILayoutAPI m_EditorGUILayout = new EditorGUILayoutAPI();
        private APIMode m_CurrentAPIMode = APIMode.GUILayout;

        #region Basic

        void Basic()
        {
            if (m_CurrentAPIMode == APIMode.GUILayout)
            {
                m_GUILayoutAPI.Draw();
            }
            else if (m_CurrentAPIMode == APIMode.GUI)
            {
                m_GUIAPI.Draw();
            }
            else if (m_CurrentAPIMode == APIMode.EditorGUI)
            {
                m_EditorGUIApi.Draw();
            }
            else if (m_CurrentAPIMode == APIMode.EditorGUILayout)
            {
                m_EditorGUILayout.Draw();
            }
        }

        #endregion

        #region Enabled

        private bool mEnableIntersactive = true;

        void Enabled()
        {
            mEnableIntersactive = GUILayout.Toggle(mEnableIntersactive, "是否可交互");
            if (GUI.enabled != mEnableIntersactive)
            {
                GUI.enabled = mEnableIntersactive;
            }

            Basic();
        }

        #endregion

        #region Rotated

        private bool mOpenRotateEffect = false;

        void Rotated()
        {
            mOpenRotateEffect = GUILayout.Toggle(mOpenRotateEffect, "旋转效果");
            if (mOpenRotateEffect)
            {
                //左上角00 往右往下增加
                GUIUtility.RotateAroundPivot(45, Vector2.one * 200);
            }

            Basic();
        }

        #endregion

        #region Scale

        private bool mOpenScaleEffect = false;

        void Scale()
        {
            mOpenScaleEffect = GUILayout.Toggle(mOpenScaleEffect, "缩放效果");
            if (mOpenScaleEffect)
            {
                GUIUtility.ScaleAroundPivot(0.5f * Vector2.one, Vector2.one * 200);
            }

            Basic();
        }

        #endregion

        #region Color

        private bool mOpenColorEffect = false;

        void Color()
        {
            mOpenColorEffect = GUILayout.Toggle(mOpenColorEffect, "变换颜色");
            if (mOpenColorEffect)
            {
                GUI.color = UnityEngine.Color.yellow;
            }

            Basic();
        }

        #endregion

        private PageId m_CurrentPageId;

        private void OnGUI()
        {
            m_CurrentAPIMode = (APIMode)(GUILayout.Toolbar((int)m_CurrentAPIMode, Enum.GetNames(typeof(APIMode))));

            //简易页面框架：
            m_CurrentPageId = (PageId)(GUILayout.Toolbar((int)m_CurrentPageId, Enum.GetNames(typeof(PageId))));
            if (m_CurrentPageId == PageId.Basic)
            {
                Basic();
            }
            else if (m_CurrentPageId == PageId.Enabled)
            {
                Enabled();
            }
            else if (m_CurrentPageId == PageId.Rotated)
            {
                Rotated();
            }
            else if (m_CurrentPageId == PageId.Scale)
            {
                Scale();
            }
            else if (m_CurrentPageId == PageId.Color)
            {
                Color();
            }
        }
    }
}